import {Axios} from '@/utils/AxiosPlugin.js'
import cookie from 'cookie'

const ENDPOINT = "/api/questionnaire/"

Axios.interceptors.response.use(function (response) {
	if (response.data.code != 200) {
		if (!response.data.detail) {
			response.data.detail = response.data.msg
		}
		throw {
			response: {
				status: response.data.code,
				data: response.data
			}
		}
	}
	return response
});

export default {
	async get(id) {
		var response = await Axios.get("/api/questionnaire/get?qnid=" + id)
		if (response.data.code === 200) {
			response.data = response.data.data
		}
		console.log(response)
		return response
	},
	async add(form) {
		var uid = cookie.parse(document.cookie).uid
		if (!uid) {
			throw {response: {data: {detail: "empty uid"}}}
		}
		return await Axios.put("/api/questionnaire/create?uid=" + uid, form)
	},
	async update(id, form) {
		return await Axios.post(ENDPOINT + id, form)
	},
	async delete(id) {
		return await Axios.delete(ENDPOINT + id)
	},
	async publish(id) {
		return await Axios.post("/api/questionnaire/publish?qnid=" + id)
	},
}
